/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.messagequeue.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.fanrenqiji.modules.business.messagequeue.vo.MessageQueueVO;
import com.fanrenqiji.modules.business.messagequeue.service.MessageQueueService;

@Controller
@RequestMapping(value = "${adminPath}/business/messagequeue")
public class MessageQueueController extends BaseController {
	
    private static Logger log = LoggerFactory.getLogger(MessageQueueController.class);
    @Autowired
	private MessageQueueService messageQueueService;

@ModelAttribute("messageQueue")
public MessageQueueVO getMessageQueue(@RequestParam(required = false) String id){
	if(StringUtils.isNotEmpty(id)){
		return messageQueueService.get(id);
	}else{
		return new MessageQueueVO();
	}
}

/**
 * 列表
 *
 * @return
 */
@RequiresPermissions("business:messagequeue:view")
@RequestMapping(value = {"list", ""})
public String list(MessageQueueVO messageQueue,HttpServletRequest request,
		HttpServletResponse response,Model model){
	Page<MessageQueueVO> page = messageQueueService.findMessageQueueByPage(new Page<MessageQueueVO>(request,response),
		messageQueue);
	model.addAttribute("page",page);
	model.addAttribute("messageQueue",messageQueue);
	return"modules/business/messagequeue/messagequeueList";
}

/**
 * 选择列表
 *
 * @return
 */
@RequestMapping(value = "select")
public String select(MessageQueueVO messageQueue,HttpServletRequest request,HttpServletResponse response,Model model){
	Page<MessageQueueVO> page = messageQueueService.findMessageQueueByPage(new Page<MessageQueueVO>(request,response,10),messageQueue);
	model.addAttribute("page",page);
	model.addAttribute("messageQueue",messageQueue);
	return"modules/business/messagequeue/messagequeueSelect";
}

/**
 * 用户添加更新表
 *
 * @param model
 * @return
 */
@RequiresPermissions("business:messagequeue:view")
@RequestMapping(value = "form")
public String form(Model model,MessageQueueVO messageQueue){
	if (StringUtils.isNotEmpty(messageQueue.getId())) {
		model.addAttribute("messageQueue", messageQueueService.get(messageQueue.getId()));
	}else{
		model.addAttribute("messageQueue",messageQueue);
	}
	return"modules/business/messagequeue/messagequeueForm";
}

/**
 * 添加或修改提交
 *
 * @param messageQueue
 * @param model
 * @param redirectAttributes
 * @return
 */
@RequiresPermissions("business:messagequeue:edit")
@RequestMapping(value = "save")
public String save(MessageQueueVO messageQueue,Model model,
		RedirectAttributes redirectAttributes){// TODO @Valid 添加后端校验
	if(!beanValidator(model,messageQueue)){
		return form(model,messageQueue);
	}
	messageQueueService.update(messageQueue);
	addMessage(redirectAttributes,"保存成功");
	return"redirect:"+GlobalSetting.getAdminPath()
		+"/business/messagequeue/?repage";
}

/**
 * 删除操作
 *
 * @return
 */
@RequestMapping("delete")
@RequiresPermissions("business:messagequeue:edit")
public String delete(String id, RedirectAttributes redirectAttributes) {
	messageQueueService.delete(id);
	addMessage(redirectAttributes, "删除成功");
	return "redirect:" + GlobalSetting.getAdminPath() + "/business/messagequeue/";
}
}